package com.qifan.energy.util;

import android.content.Context;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Build;

import com.orhanobut.logger.Logger;
import com.qifan.energy.R;

public class SoundPoolUtil {

    private static SoundPoolUtil soundPoolUtil;

    private SoundPool soundPool;

    //单例模式
    public static SoundPoolUtil getInstance(Context context) {
        if (soundPoolUtil == null){
            soundPoolUtil = new SoundPoolUtil(context);
        }
        return soundPoolUtil;
    }

    private SoundPoolUtil(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//>21
            soundPool = new SoundPool.Builder()
                    .setMaxStreams(10)//最多播放音频数量
                    .setAudioAttributes(new AudioAttributes.Builder().setLegacyStreamType(AudioManager.STREAM_MUSIC).build())
                    .build();
        } else {
            soundPool = new SoundPool(3, AudioManager.STREAM_MUSIC, 0);
        }

        //加载音频文件
        //soundPool.load(context, R.raw.click, 1);
        soundPool.load(context, R.raw.onclick, 1);
        soundPool.load(context, R.raw.end, 1);
        soundPool.load(context, R.raw.level_tips, 1);
    }

    private void play(int number) {
        Logger.i("tag", "number " + number);
        //播放音频
        soundPool.play(number, 1, 1, 0, 0, 1);
    }

    public void onClick(){
        play(1);//默认1
    }

    public void onResult(){
        play(2);//开注结果音效
    }

    public void onLevelTips(){
        play(3);//等级提醒
    }
}
